<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 搜索模块
 *
 * @author      Eden
 * @datetime    2016/12/17 17:14
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Search extends ADM_Controller
{
    const PAGE_SIZE = 10;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 页面
     */
    public function index()
    {

        $this->load->view(parent::TMP_DIR . '/system/search.html');
    }

    public function add()
    {
        $send['state'] = false;
        do {

            $params = $this->input->post();
            if (empty($params['keyword'])) {
                $send['error'] = '关键字不能为空';
                break;
            }

            if (!isset($params['times'])){
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/system/Search_Model', 's');
            if($res = $this->s->add($params)) {
                $send['state'] = true;
                $send['data'] = $res;
            } else {
                $send['error'] = '关键词添加失败';
            }

        } while(false);
        $this->output->send($send);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $send['state'] = false;
        do {

            $params = $this->input->post();
            if (empty($params['kId']) || empty($params['type'])) {
                $send['error'] = '参数错误';
                break;
            }

            if ($params['type'] == 'keyword' && empty($params['keyword'])) {
                $send['error'] = '关键字不能为空';
                break;
            }

            if ($params['type'] == 'times' && !isset($params['times'])){
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/system/Search_Model', 's');
            if($this->s->edit($params)) {
                $send['state'] = true;
            }

        } while(false);
        $this->output->send($send);
    }

    /**
     * 获取列表
     */
    public function getLists()
    {
        $send['state'] = false;
        do {
            $page = $this->input->get('page');
            $page = !empty($page) && intval($page) > 0 ? intval($page) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $this->load->model('admin/system/Search_Model', 's');
            $res = $this->s->getKeywords($offset, self::PAGE_SIZE);

            if ($res && !empty($res['total'])) {
                $send['state'] = true;
                $send['data'] = array(
                    'nextPage' => ($offset + self::PAGE_SIZE) < $res['total'],
                    'lists' => $res['data']
                );
            } else {
                $send['error'] = '加载失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 删除
     */
    public function removeItem()
    {
        $send['state'] = false;
        do {

            $params = $this->input->get();
            if (empty($params['kId']) || empty($params['action']) || $params['action'] !== 'removeItem') {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/system/Search_Model', 's');
            if($this->s->remove($params['kId'])) {
                $send['state'] = true;
            }

        } while(false);
        $this->output->send($send);
    }

}
